package com.sskj.candy.presenter;

import android.view.View;

import com.lzy.okgo.model.Response;
import com.sskj.candy.R;
import com.sskj.candy.bean.BalanceCqtfBean;
import com.sskj.candy.bean.CandyBean;
import com.sskj.candy.bean.CandyRankBean;
import com.sskj.candy.bean.CandyRecordBean;
import com.sskj.candy.bean.CandyTextBean;
import com.sskj.candy.bean.Water;
import com.sskj.candy.http.HttpConfig;
import com.sskj.candy.ui.fragment.CandyFragment;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.functions.Consumer;


public class CandyFragmentPresenter extends BasePresenter<CandyFragment> {

    /**
     * 领取红利
     *
     * @param id 红利记录id
     * @return
     */
    public void getSuger(Water water,View view, String id) {
        httpService.getSuger(id).execute(new JsonCallBack<HttpData>() {
            @Override
            public void onSuccess(Response<HttpData> response) {
                HttpData httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {
                    mView.removeView(water,view);
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.candy_candyFragmentPresenter1_j));
                }
            }
        });
    }


    /**
     * 排行榜
     *
     * @param p    页码，默认1
     * @param size 每页多少条，默认15
     * @return
     */
    public void getSugerRank(int p, int size) {
        httpService.getSugerRank(p, size).execute(new JsonCallBack<HttpData<CandyBean>>() {
            @Override
            public void onSuccess(Response<HttpData<CandyBean>> response) {
                HttpData<CandyBean> httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {
                    mView.update(httpData.getData());
                }
            }

            @Override
            public void onError(Response<HttpData<CandyBean>> response) {
                mView.stopRefresh();
                super.onError(response);
            }
        });
    }

    /**
     * 红利记录
     *
     * @param status 1未领取 2已领取，默认全部
     * @return
     */
    public void getSugerRecord(int status) {
        httpService.getSugerRecord(status).execute(new JsonCallBack<HttpData<PageBean<CandyRecordBean>>>() {
            @Override
            public void onSuccess(Response<HttpData<PageBean<CandyRecordBean>>> response) {
                HttpData<PageBean<CandyRecordBean>> httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {
                    List<Water> listWater = new ArrayList<>();
                    for (int i = 0; i < httpData.getData().getList().size(); i++) {
                        Water water = new Water();
                        water.setId(httpData.getData().getList().get(i).getId());
                        water.setUsdt_num(httpData.getData().getList().get(i).getNum());
                        listWater.add(water);
                    }
                    mView.updateWaterList(listWater);
                }
            }
        });
    }

    /**
     * 轮播通知
     *
     * @return
     */
    public void finanBanner() {
        httpService.getCandyBanner().execute(new JsonCallBack<HttpData<CandyTextBean>>() {
            @Override
            public void onSuccess(Response<HttpData<CandyTextBean>> response) {
                HttpData<CandyTextBean> httpData = response.body();
                ToastUtil.showShort(httpData.getMsg());
                if (httpData.getStatus() == HttpConfig.OK) {
                    mView.updateBanner(httpData.getData());
                }
            }
        });
    }

    public void getBalance() {
        httpService.getBalance()
                .execute(new JsonCallBack<HttpData<BalanceCqtfBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<BalanceCqtfBean>> response) {
                        HttpData<BalanceCqtfBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateBalance(httpData.getData());
                        }
                    }
                });
    }
}
